# flask web 网络编程文件
from flask import Flask, render_template, request, escape
from cat import catfind, key1
from dog import dogfind, key2
from plants import plantsfind, key3
from water import waterfind, key4
from vsearch import log_request
# html 调用方法：render_template  按照 jinja2 规则
#

app = Flask(__name__)


@app.route('/')
@app.route('/hello', methods=['POST','GET'])
def hello():
    title = '欢迎~'
    return render_template("hello.html",
                           the_title=title,)
@app.route('/cat', methods=['POST','GET'])
def cat() -> 'html':
    title = 'Cat'
    return render_template('cat.html',
                           the_title=title)
@app.route('/cat_results', methods=['POST','GET'])
def cat_results() -> 'html':
  apiKey = '5AGZczH538c065ca18b2353370bb64384a85237067281d1'
  keyword = 'keyword'
  title = '这是你的查询结果哦'
  results = catfind(apiKey,keyword)
  log_request(request, results)
  return render_template('cat_results.html',
                          the_title=title,
                          the_name=results,
                          the_coverURL=results,
                          the_engName=results,
                          the_price=results

  )

@app.route('/dog', methods=['POST','GET'])
def dog() -> 'html':
    title = 'Dog'
    return render_template('dog.html',
                           the_title=title)
@app.route('/dog_results', methods=['POST','GET'])
def dog_results() -> 'html':
  apiKey = '5AGZczH538c065ca18b2353370bb64384a85237067281d1'
  keyword = 'keyword'
  title = '这是你的查询结果哦'
  results = dogfind(apiKey,keyword)
  log_request(request, results)
  return render_template('dog_results.html',
                          the_title=title,
                          the_name=results,
                          the_coverURL=results,
                          the_engName=results,
                          the_price=results

  )

@app.route('/plants', methods=['POST','GET'])
def plants() -> 'html':
    title = 'Plants'
    return render_template('plants.html',
                           the_title=title)
@app.route('/plants_results', methods=['POST','GET'])
def plants_results() -> 'html':
  apiKey = '5AGZczH538c065ca18b2353370bb64384a85237067281d1'
  keyword = 'keyword'
  title = '这是你的查询结果哦'
  results = plantsfind(apiKey,keyword)
  log_request(request, results)
  return render_template('plants_results.html',
                          the_title=title,
                          the_name=results,
                          the_coverURL=results,
                          the_engName=results,
                          the_price=results,
                          the_area=results,

  )

@app.route('/water', methods=['POST','GET'])
def water() -> 'html':
    title = 'Water'
    return render_template('water.html',
                           the_title=title)
@app.route('/water_results', methods=['POST','GET'])
def water_results() -> 'html':
  apiKey = '5AGZczH538c065ca18b2353370bb64384a85237067281d1'
  keyword = 'keyword'
  title = '这是你的查询结果哦'
  results = waterfind(apiKey,keyword)
  log_request(request, results)
  return render_template('water_results.html',
                          the_title=title,
                          the_name=results,
                          the_coverURL=results,
                          the_engName=results,
                          the_price=results

  )



@app.route('/viewlog', methods=['POST','GET'])
def view_the_log() -> 'html':
    """用户日志"""
    contents = []
    with open('viewlog.log','r') as log:
        for line in log:
            contents.append([])
            for item in line.split('|'):
                contents[-1].append(escape(item))
    len(contents)
    titles = ('用户提交','远程地址','访问代理','用户结果')
    return render_template('viewlog.html',
                           the_title='日志系统',
                           the_data=contents)


if __name__ == '__main__':
    app.run(debug=True)
